﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using JsMapper.BO;

#endregion

namespace JsMapper.BL
{
    public class JsModelRepository
    {
        public JsModelRepository()
        {
            Repository = new List<IJsModel>();
        }

        private List<IJsModel> Repository { get; set; }

        public void Add<T>() where T : class
        {
            var m = JsModelBuilder.Create<T>();
            Repository.Add(m);
        }

        public void Add(Type t)
        {
            var meth = GetType().GetMethod("Add", Type.EmptyTypes);
            var genMeth = meth.MakeGenericMethod(new[] {t});

            genMeth.Invoke(this, null);
        }

        public void AddRange(IEnumerable<Type> types)
        {
            foreach (var t in types)
            {
                Add(t);
            }
        }

        public JsModel<T> Get<T>() where T : class
        {
            return Repository.OfType<JsModel<T>>().First();
        }

        public IJsModel Get(Type t)
        {
            return Repository.FirstOrDefault(x => x.ModelType == t);
        }

        public IEnumerable<IJsModel> GetAll()
        {
            return Repository;
        }
    }
}